package com.zhhs.galaxyott.activity.moreMoviesActivity;

import java.lang.reflect.Field;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhhs.galaxyott.R;
import com.zhhs.galaxyott.activity.HotMovieActivity;
import com.zhhs.galaxyott.activity.MoreSpecialActivity;
import com.zhhs.galaxyott.activity.SingleSpecialActivity;
import com.zhhs.galaxyott.activity.movieDetailActivity.MovieDetailActivity;
import com.zhhs.galaxyott.adapters.ListFragmentAdapter;
import com.zhhs.galaxyott.adapters.MoviesPagersAdapter;
import com.zhhs.galaxyott.beans.HomeMovieBean;
import com.zhhs.galaxyott.customview.VerticalViewPager;
import com.zhhs.galaxyott.customview.myListView;
import com.zhhs.galaxyott.utils.Logs;
import com.zhhs.galaxyott.utils.ViewPagerScroller;

public class MoreMoviesHolder implements OnClickListener {

    private Activity mContext;

    public static myListView mListView;
    public static LinearLayout mtitleLayout;
    private ListFragmentAdapter mlistAdapter;
    public static ImageView imageLeft, imageRight;
    private ImageView type_img;

    protected MoviesPagersAdapter mVpAdapter;

    // DirectionalViewPager mViewPager;
    VerticalViewPager mViewPager;

    private List<HomeMovieBean> specialData;

    public MoreMoviesHolder(Activity context) {

        this.mContext = context;

        initView();

    }

    private void initView() {
        type_img = (ImageView) mContext.findViewById(R.id.type_img);
        type_img.setBackgroundResource(R.drawable.movielist_img);

        imageLeft = (ImageView) mContext.findViewById(R.id.iv_activity_left);
        imageRight = (ImageView) mContext.findViewById(R.id.iv_activity_right);

        mListView = (myListView) mContext.findViewById(R.id.title_layout);
        // mViewPager = (DirectionalViewPager)
        // mContext.findViewById(R.id.DirectionalViewPager);
        // mViewPager.setOrientation(DirectionalViewPager.VERTICAL);
        mViewPager = (VerticalViewPager) mContext.findViewById(R.id.DirectionalViewPager);

        initViewPagerScroll();

    }

    /**
     * 
     * 设置ViewPager的滑动速度
     * 
     * */

    private void initViewPagerScroll() {

        try {
            Field mScroller = null;
            mScroller = ViewPager.class.getDeclaredField("mScroller");
            mScroller.setAccessible(true);
            ViewPagerScroller scroller = new ViewPagerScroller(mViewPager.getContext());

            mScroller.set(mViewPager, scroller);

        } catch (NoSuchFieldException e) {

        } catch (IllegalArgumentException e) {

        } catch (IllegalAccessException e) {

        }

    }

    // 注：在切换title的时候初始化更好
    private OnPageChangeListener mOnPageChangeListener = new OnPageChangeListener() {

        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (positionOffsetPixels == 0 && positionOffset == 0) {
                // 在这里面刷新数据
                Logs.logD("onPageScrolled", "onPageScrolled--" + position);
                mVpAdapter.initViews(position);
            }

        }
        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
        }
    };

    public void setListener(MoviesPagersAdapter mVpAdapter) {

        this.mVpAdapter = mVpAdapter;
        mListView.setOnItemSelectedListener(mOnItemSelectedListener);

        // mViewPager.setOnPageChangeListener(mOnPageChangeListener);

    }

    public static int lastSelect = 0;
    OnItemSelectedListener mOnItemSelectedListener = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View v, int position, long arg3) {

            Logs.logD("DEBUG_DT", "THIS IS onItemSelected-->" + position);

            if (MoreMoviesActivity.isFirstLoad) {
                Logs.logD("DEBUG_DT", "THIS IS isFirstLoad-->true");

                MoreMoviesActivity.isFirstLoad = false;
                MoreMoviesActivity.selectId = MoreMoviesActivity.pos;

                mVpAdapter.initViews(MoreMoviesActivity.pos);

                // mViewPager.setCurrentItem(pos);

                ((TextView) ((FrameLayout) mListView.getChildAt(MoreMoviesActivity.pos)).getChildAt(0))
                        .setBackgroundResource(R.drawable.title_focus);

            } else {
                // Logs.logD("DEBUG_DT", "THIS IS isFirstLoad-->false");
                MoreMoviesActivity.selectId = position;

                // mVpAdapter.initViews(position);
                // mViewPager.setCurrentItem(mListView.getSelectedItemPosition());

            }

            mListView.setSelection(MoreMoviesActivity.selectId);
            mListView.getChildAt(MoreMoviesActivity.selectId).requestFocus();

            /******** 方案二 *******/
            // 实现title背景变化与 mGridViews.getViewTreeObserver()配合使用
            // Logs.logD("DEBUG_DT", "THIS IS -->lastSelect:" + lastSelect +
            // "selectId:" + MoreMoviesActivity.selectId);
            // ((TextView) ((FrameLayout)
            // mListView.getChildAt(lastSelect)).getChildAt(0))
            // .setBackgroundResource(R.drawable.rect_transparent_bg);
            // ((TextView) ((FrameLayout)
            // mListView.getChildAt(MoreMoviesActivity.selectId)).getChildAt(0))
            // .setBackgroundResource(R.drawable.title_focus);
            /******** end *******/

        }
        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    };

    public void setSpecialListener(List<HomeMovieBean> specialData) {
        this.specialData = specialData;
        imageLeft.setOnClickListener(this);
        imageRight.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.iv_activity_left:
            if (specialData.size() >= 0) {
                startActivity(specialData.get(0));
            }

            break;
        case R.id.iv_activity_right:
            if (specialData.size() >= 1) {
                startActivity(specialData.get(1));
            }

            break;

        default:
            break;
        }
    }

    public void startActivity(HomeMovieBean homeMovieBean) {
        Intent intent = new Intent();
        int type = homeMovieBean.getAdType();
        if (type == 1) {
            intent.setClass(mContext, MovieDetailActivity.class);
            intent.putExtra("movieid", homeMovieBean.getObjectid());
            mContext.startActivity(intent);

        } else if (type == 2) {
            // intent.setClass(MoreMoviesActivity.this, HotMovieActivity.class);
            // this.startActivity(intent);

        } else if (type == 3) {
            intent.setClass(mContext, MoreSpecialActivity.class);

            mContext.startActivity(intent);

        } else if (type == 4) {
            intent.setClass(mContext, SingleSpecialActivity.class);
            intent.putExtra("specailId", homeMovieBean.getObjectid());
            mContext.startActivity(intent);

        } else if (type == 5) {
            intent.setClass(mContext, HotMovieActivity.class);
            intent.putExtra("specailId", homeMovieBean.getObjectid());
            mContext.startActivity(intent);

        }
    }

}
